Myblog::App.controllers :personal do


  layout :homepage

  # get :index, :map => '/foo/bar' do
  #   session[:foo] = 'bar'
  #   render 'index'
  # end

  # get :sample, :map => '/sample/url', :provides => [:any, :js] do
  #   case content_type
  #     when :js then ...
  #     else ...
  # end

  # get :foo, :with => :id do
  #   "Maps to url '/foo/#{params[:id]}'"
  # end

  # get '/example' do
  #   'Hello world!'
  # end

  before :index do
    if !current_user
      redirect url(:login, :index)
    end
  end

  get :index do
    @personal = "active"
    if current_user.balance
      @balance = current_user.balance
    else
      @balance = Balance.new
      @balance.currency_big = 0;
      @balance.currency_small = 0;
      @balance.amount = '0.00';
      current_user.balance = @balance;
      @balance.save
    end
    if @balance.bills
      @bills = @balance.bills.order_by(created_at: "DESC")
    else
      @bills = []
    end
    if current_user.orders
      @orders = current_user.orders
    else
      @orders = []

    end
    @user = User.find(current_user.id)
    render 'personal/index'
  end

  post :update do
    if current_user
      @user = User.find(current_user.id)
      if params[:username]
        @user.username = params[:username]
      else
        @user.username = current_user.mobile
      end
      @user.email = params[:email]
      @user.mobile = params[:mobile]
      @user.fake_name = params[:fake_name]
      @user.real_name = params[:real_name]
      @user.personal_title = params[:personal_title]
      if @user.save
        redirect url('/personal')
      end

    else
      redirect url('/login/index')
    end
  end


  post :updatepass do
    if current_user
      if params[:old_password]
        @user = User.find(current_user.id)
        if !@user.has_password?(params[:old_password])
          @message = "旧密码没有认证"
          redirect url('personal'), :error => @message.html_safe+'<i class="close icon"></i>'.html_safe
        end
      end
      @user = User.find(current_user.id)
      @user.password = params[:password]
      @user.password_confirmation = params[:password_confirmation]
      if @user.save!
        @message = "密码修改成功！"
        redirect url('/personal'), :success => @message.html_safe+'<i class="close icon"></i>'.html_safe
      else
        redirect url('/login/index')
      end
    else
      redirect url('/login/index')
    end
  end

  post :require_withdraw do
    #请求提现
    if !current_user
        @message = "请先登录！"
        redirect url('/login/index'), :error => @message.html_safe+'<i class="close icon"></i>'.html_safe
    end

    @balance = Balance.where(user_id: current_user.id).first
    if params[:amount]
      @bill = Bill.new
      @bill.cash_type = "提现"
      @bill.amount = params[:amount].to_i*100
      @bill.bank_card = params[:bank_card_id]
      @bill_bank = @bill.bank_card
      @bill.info = "对户名为:#{@bill_bank.bank_real_name}——提现业务, 账户为：#{@bill_bank.bank_address}|卡号：#{@bill_bank.card_number}"
      @bill.status = 2
      @bill.user_to = current_user.id
      @bill.balance = @balance
      if @bill.save!
        @message = '提现发起成功'
        redirect url(:personal, :index), :success => @message.html_safe+'<i class="close icon"></i>'.html_safe

      else
        @message = "数据失败，请重试"
        redirect url(:personal, :index), :success => @message.html_safe+'<i class="close icon"></i>'.html_safe

      end
    end
  end


  get :bank_exists do
    if  User.find(current_user.id).bank_cards.length > 0
      1.to_s
    else
      0.to_s
    end

  end

  get :bank_cards do
    @personal = "active"
    @bankcards = BankCard.where(user_id: current_user.id).order_by(created_at: "DESC")
    render "personal/bank_cards"
  end

  get :new_bank_card do
    @personal = "active"
    @bankcard = BankCard.new
    render "personal/new_bank_card"
  end

  get :edit_bank_card, :with => :id do
    @personal = "active"
    @bankcard = BankCard.find(params[:id])
    render 'personal/edit_bank_card'
  end

  post :create_new_bank_card do
    @user = User.find(current_user.id)
    @card = BankCard.new
    @card.card_number = params[:card_number]
    @card.bank_address = params[:bank_address]
    @card.bank_mobile = params[:bank_mobile]
    @card.bank_real_name = params[:bank_real_name]
    @card.user = @user
    if @card.save!
      @message = "银行卡增加成功"
      redirect url(:personal, :bank_cards), :success => @message.html_safe+'<i class="close icon"></i>'.html_safe
    else
      @message = "数据失败，请重试"
      redirect url(:personal, :bank_cards), :error => @message.html_safe+'<i class="close icon"></i>'.html_safe
    end
  end

  post :update_bank_card do
    @card = BankCard.find(params[:bank_id])
    @card.card_number = params[:card_number]
    @card.bank_address = params[:bank_address]
    @card.bank_mobile = params[:bank_mobile]
    @card.bank_real_name = params[:bank_real_name]
    if @card.save!
      @message = "银行卡更新成功"
      redirect url(:personal, :bank_cards), :success => @message.html_safe+'<i class="close icon"></i>'.html_safe
    else
      @message = "数据失败，请重试"
      redirect url(:personal, :bank_cards), :error => @message.html_safe+'<i class="close icon"></i>'.html_safe
    end
  end

  get :delete_bank_card, :with => :id do
    @personal = "active"
    @card = BankCard.find(params[:id])
    if @card.destroy!
      @message = "银行卡成功删除"
      redirect url(:personal, :bank_cards), :success => @message.html_safe+'<i class="close icon"></i>'.html_safe
    else
      @message = "数据失败，请重试"
      redirect url(:personal, :bank_cards), :error => @message.html_safe+'<i class="close icon"></i>'.html_safe
    end
  end

end
